var NewsAdd = function() {

	var handleValidation = function() {
		var formAdd = $('#addForm');
		var validator = formAdd.validate({
			rules: {
				title: {
					required: true,
					rangelength: [1, 50]
				},
				orders: {
					required: true,
					number: true
				},
				platform: {
					required: true
				}
			},
			messages: {
				title: {
					required: "新闻标题不能为空",
					rangelength: "最大长度为50"
				},
				orders: {
					required: "排序值不能为空",
					number: "请输入一个数字"
				},
				platform: {
					required: "发布平台不能为空"
				}
			},
			success: function(label, element) {
				var icon = $(element).parent('.input-icon').children(
					'i');
				$(element).closest('.form-group').removeClass(
					'has-error').addClass('has-success');
				icon.removeClass("fa-warning").addClass("fa-check");
			},

			submitHandler: function(form) {
				var content = $('.summernote').code();
				var status = $("#status").find("option:selected").val();
				var thumbnail = $("#thumbnail").val();
				
				if(thumbnail==null||thumbnail==""){
					bootbox.alert("请添加一张新闻封面图片");
					return false;
				}
				if(content==null||content==""||content=="<p><br></p>"){
					bootbox.alert("请添加新闻内容");
					return false;
				}
				if(content.length>20000){
					bootbox.alert("你的新闻太长了，请输入少于1万个字符");
					return false;
				}
				var json = $('form#addForm').serializeObject();
				json.content=content;
				$('#addModel').attr('disabled',true);
				$.ajax({
					type: "post",
					url: ctx + "/news/doAdd",
					data: json,
					success: function(msg) {
						if (msg.code == 1) {
							bootbox.dialog({
								message: "添加新闻成功！",
								title: "温馨提示：",
								buttons: {
									main: {
										label: "确定",
										className: "btn btn-primary",
										callback: function() {
											window.location.href = 'news/list';
										}
									}
								}
							});
							$('#addModel').attr('disabled',false);
						} else {
							bootbox.dialog({
								message: msg.message,
								title: "温馨提示：",
								buttons: {
									main: {
										label: "确定",
										className: "btn btn-primary",
										callback: function() {
//											window.location.reload();
										}
									}
								}
							});
							$('#addModel').attr('disabled',false);
						}
					},
					error: function(msg) {
						bootbox.alert("添加新闻失败！");
						$('#addModel').attr('disabled',false);
					}
				});
			}
		});
	}

	return {
		init: function() {
			handleValidation();
		}
	};
}();

function sendFile(file, editor, welEditable) {
	data = new FormData();
	data.append("file", file);
	$.ajax({
		data: data,
		type: "POST",
		url: ctx + "/news/uploadFile",
		cache: false,
		contentType: false,
		processData: false,
		success: function(url) {
			// editor.insertImage(welEditable, url.data);
			$(".summernote").summernote("insertImage",imagePrefix + url.data, "aaa");
		}
	});
}

$("#file").change(
	function() {
		data = new FormData();
		data.append("file", this.files[0]);
		$.ajax({
			data: data,
			type: "POST",
			url: ctx + "/news/uploadFile",
			cache: false,
			contentType: false,
			processData: false,
			success: function(msg) {
				if (msg.code == 1) {
					$("#thumbnail").val(msg.data);
					$("#adsImg").html(
						'<img src="' + imagePrefix + msg.data + '" width="256">');
				} else {
					bootbox.alert("添加图片失败！");
				}
			},
			error: function(msg) {
				bootbox.alert(msg.message);
			}
		});
	});
